package hiring.tong.com.company.servlet;

import java.io.IOException;
import java.text.ParseException;



import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import hiring.tong.com.common.Constant;
import hiring.tong.com.common.PageModel;
import hiring.tong.com.company.dao.ApplicationDao;
import hiring.tong.com.company.entity.ApplicationEntity;
import hiring.tong.com.company.entity.CompanyEntity;
import hiring.tong.com.talent.entity.TalentEntity;

/**
 * Servlet implementation class ApplicationServlet
 */
public class ApplicationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ApplicationDao applicationDao=new ApplicationDao();
   

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String command=request.getParameter("command");
		if("list".equals(command)){
			list(request,response);	
		}
		else if("apply".equals(command)){
			apply(request,response);	
		}
		else if("ApplicationList".equals(command)){
			try {
				ApplicationList(request,response);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		}
		else if("verify".equals(command)){
			verify(request,response);	
		}
		
		else{
			throw new RuntimeException("非法操作");
		}
	}
	private void verify(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		int applicationId=Integer.parseInt(request.getParameter("applicationId"));
		int status=Integer.parseInt(request.getParameter("status"));
		int pageNo=Integer.parseInt(request.getParameter("pageNo"));
		int row=applicationDao.verify(applicationId,status);
		if(row==1){
			 String pretime=request.getParameter("pretime");
		        String nexttime=request.getParameter("nexttime");
			response.sendRedirect("ApplicationServlet?command=ApplicationList&pageNo="+pageNo+"&pretime="+pretime+"&nexttime="+nexttime);
		}
	}

	private void ApplicationList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
		// TODO Auto-generated method stub
		CompanyEntity company=(CompanyEntity)request.getSession().getAttribute("company");
		int company_id=company.getId();
	
	 String pretime=null;
	 String nexttime=null;
	 if(request.getParameter("pretime")==null&&request.getParameter("nexttime")==null){
		 pretime=null;
		 nexttime=null;
	 }
	 else if(request.getParameter("pretime")!=null&&!"".equals(request.getParameter("pretime"))&&"".equals(request.getParameter("nexttime"))){
		 pretime=request.getParameter("pretime");
		 nexttime="";
	 }
	 else if("".equals(request.getParameter("pretime"))&&request.getParameter("nexttime")!=null&&!"".equals(request.getParameter("nexttime"))){
		 pretime="";
		 nexttime=request.getParameter("nexttime");
	 }
	 else{
		 pretime= request.getParameter("pretime");
		 nexttime=request.getParameter("nexttime");		 
	 }
	String positionname=request.getParameter("positionname");
	 
	 String viref=request.getParameter("viref");
	 System.out.println(viref);
	 
	 
		int pageNo=Constant.DEFAULT_PAGE_NO;
		String pageStr=request.getParameter("pageNo");
		if(pageStr!=null&&Integer.parseInt(pageStr)>1){
			pageNo=Integer.parseInt(pageStr);
		}
		
		
		ApplicationEntity application=new ApplicationEntity();
		if(viref!=null&&!"".equals(request.getParameter("viref"))){
			 int status=Integer.parseInt(request.getParameter("viref"));
			 application.setStatus(status);
		 }
		application.setCompany$id(company_id);
		application.setPretime(pretime);
		application.setNexttime(nexttime);
		application.setPosition$name(positionname);
		PageModel<ApplicationEntity> pagemodel=applicationDao.ApplicationList(application,pageNo,Constant.DEFAULT_PAGE_SIZE);
		
		request.setAttribute("pagemodel", pagemodel);
		request.setAttribute("applications", application);
	       
	
		request.getRequestDispatcher("/view/company/company-application.jsp").forward(request, response); 
		
		
		
		
		
		
	}

	private void apply(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		TalentEntity talent=(TalentEntity)request.getSession().getAttribute("talent");
		int talent_id=talent.getId();
		int position_id=Integer.parseInt(request.getParameter("positionId"));
		int resume_id=Integer.parseInt(request.getParameter("resumeId"));
		
		int row =applicationDao.add(position_id,talent_id,resume_id);
		if (row == 1) {
			request.setAttribute("success", "求职者的简历投递成功~");
		} else {
			request.setAttribute("error", "求职者的简历投递失败~");
		}
		// 回显信息
		response.sendRedirect("PositionServlet?command=get&id=" + position_id);
	}

	//查询用户的申请信息
	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		TalentEntity talent=(TalentEntity)request.getSession().getAttribute("talent");
		int talent_id=talent.getId();
		
		int pageNo=Constant.DEFAULT_PAGE_NO;
		String pageStr=request.getParameter("pageNo");
		if(pageStr!=null&&Integer.parseInt(pageStr)>1){
			pageNo=Integer.parseInt(pageStr);
		}
		ApplicationEntity application=new ApplicationEntity();
		application.setTalentId(talent_id);
		PageModel<ApplicationEntity> pagemodel=applicationDao.list(application,pageNo,Constant.DEFAULT_PAGE_SIZE);
		request.setAttribute("pagemodel", pagemodel);
		request.getRequestDispatcher("view/talent/talent-application.jsp").forward(request, response); 
		
	}

}
